package com.idna.batchid.batch.job;

import java.util.Iterator;
import java.util.List;

import org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader;

import com.idna.batchid.dao.RecordDao;
import com.idna.batchid.domain.Record;


public class DatabaseReader extends AbstractItemCountingItemStreamItemReader<Record>
{
	private RecordDao<String, Record> dao;
	private List<Record> records;

	public void setDao(RecordDao<String, Record> dao)
	{
		this.dao = dao;
	}

	@Override
	protected void doClose() throws Exception
	{
		// TODO Auto-generated method stub

	}

	@Override
	protected void doOpen() throws Exception
	{
		records = dao.getAll();
	}

	@Override
	protected Record doRead() throws Exception
	{
		Iterator<Record> it = records.iterator();

		while (it.hasNext())
		{
			return records.get(getCurrentItemCount());
		}

		return null;
	}
}
